package at.kokoni.Koko_Ni;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import at.kokoni.Koko_Ni.util.AsyncLayer;
import at.kokoni.Koko_Ni.util.RestUtil;
import com.google.gson.JsonParser;
import com.google.gson.JsonObject;

public class MainActivity extends Activity {
	private ProgressDialog loadingDialog;
	private Intent intent;
	private AlertDialog.Builder alertDialogBuilder;
	private boolean isWebViewvisible = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginlayout);
		SharedPreferences preferences = getPreferences(0);
		String email = preferences.getString("email", null);
		if (email != null) {
			((EditText) findViewById(R.id.emailTextfield)).setText(email);
			findViewById(R.id.passwordTextfield).requestFocus();
		}
	}

	public void onLoginButtonClicked(View view) {
		String email = ((EditText) findViewById(R.id.emailTextfield)).getText().toString();
		String password = ((EditText) findViewById(R.id.passwordTextfield)).getText().toString();
		if (email.isEmpty() || password.isEmpty()) {
			return;
		}
		loadingDialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
		loadingDialog.setMessage("Logging in...");
		loadingDialog.setCancelable(false);
		loadingDialog.show();
		alertDialogBuilder = new AlertDialog.Builder(this);
		intent = new Intent(this, MapActivity.class);
		new AsyncLayer() {
			protected void onPostExecute(String string) {
				super.onPostExecute(string);
				JsonParser parser = null;
				JsonObject object = null;
				try {
					parser = new JsonParser();
					object = (JsonObject) parser.parse(string);
				} catch (Exception e) {
					loadingDialog.dismiss();
					alertDialogBuilder.setTitle("Error")
							.setMessage("Unable to login")
							.setCancelable(false)
							.setNeutralButton("Ok", null).create().show();
					return;
				}
				int returnValue = object.getAsJsonPrimitive("return").getAsInt();
				loadingDialog.dismiss();
				if (returnValue == 0) {
					SharedPreferences settings = getPreferences(0);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("email", ((EditText) findViewById(R.id.emailTextfield)).getText().toString());
					editor.commit();
					String sessionid = object.getAsJsonObject("data").getAsJsonPrimitive("session").getAsString();
					intent.putExtra("session", sessionid);
					startActivity(intent);
				} else {
					alertDialogBuilder.setTitle("Error")
							.setMessage(object.getAsJsonPrimitive("error").getAsString())
							.setCancelable(false)
							.setNeutralButton("Ok", null).create().show();
				}
			}
		}.execute("get", "https://BASEURL/login/" + email + "/" + password);
	}

	public void onRegisterButtonClicked(View view) {
		setContentView(R.layout.registerlayout);
		this.isWebViewvisible = true;
		WebView webView = (WebView) findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true);

		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (url.equals("https://BASEURL/index.php")) {
					setContentView(R.layout.loginlayout);
					isWebViewvisible = false;
					return true;
				}
				return false;
			}
		});
		webView.loadUrl("https://BASEURL/register.php");
	}

	@Override
	public void onBackPressed() {
		if (this.isWebViewvisible) {
			setContentView(R.layout.loginlayout);
			this.isWebViewvisible = false;
		} else {
			super.onBackPressed();
		}
	}
}
